package com.sinszm.web.exception;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 捕获非Spring的异常拦截器
 *
 * @author chenjianbo
 */
public class GlobalErrorPageInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        int status = response.getStatus();
        final boolean reqErr = 4 == status / 100;
        final boolean serverErr = 5 == status / 100;
        if (reqErr || serverErr) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            response.sendRedirect("/szm/err_" + response.getStatus());
            return false;
        }
        return super.preHandle(request, response, handler);
    }

}